<?php
//网站管理员控制器
	namespace Application\Admin\Controller;
	use Application\Admin\Model\User;
	use Application\Admin\Model\Auth;
	class Manager extends Admin{
		public function index(){			
			$user = new User();
			$this->user = $user -> order('id asc') -> select();
			$this->render();
		}		
		
		public function add(){
			if(IS_GET){
				$this->render('add');
			}
			if(IS_POST){
				
				if($_POST['username'] == ''){
					echo "<script>alert('用户名和密码不能为空');
					window.location.href='index.php?m=admin&c=manager&a=add';</script>";
					exit;
				}

				if($_POST['password'] == $_POST['password_confirm']){
					$user = new User();
					$user -> attributes -> username = $_POST['username'];
					$user -> attributes -> userpass = md5($_POST['password']);
					$user -> attributes -> tel = $_POST['tel'];
					$user -> attributes -> ctime = time();
					if($user -> save()){
						echo "<script>alert('添加成功');
						window.location.href='index.php?m=admin&c=manager&a=index';</script>";
					}
				}else{
						echo "<script>alert('密码错误！请重新输入');
						window.location.href='index.php?m=admin&c=manager&a=add';</script>";	
				}

			}
		}		
		
		public function delete(){
			if(IS_GET){
				$id = $_GET['id'];
				$user = new User();
				$user = $user ->  findModel($id);
				if($user -> delete()){
					echo "<script>alert('删除成功');
					window.location.href='index.php?m=admin&c=manager&a=index';</script>";
				}
			}
		}		
		
		public function edit(){
			if(IS_GET){
				$id = $_GET['id'];
				$user = new User();
				$this->user = $user -> find($id);
				$this->render('edit');
			}
			if(IS_POST){
				$id = $_POST['id'];
				$user = new User();
				$user = $user -> findModel($id);
				if($_POST['password'] == $_POST['password_confirm']){
					$user -> attributes -> userpass = md5($_POST['password']);
				}else{
						echo "<script>alert('密码错误！请重新输入');
						window.history.back(-1);</script>";	
						exit;
				}
				$user -> attributes ->username = $_POST['username'];
				$user -> attributes -> tel = $_POST['tel'];
				$user -> attributes -> ctime = time();
				if($user -> save()){
					echo "<script>alert('编辑成功');
					window.location.href='index.php?m=admin&c=manager&a=index';</script>";
				}
			}
		}		
	
	
//管理员权限变动		
		public function jurisdiction(){
			
			if(IS_GET){
				$userAuth = array();
				$id = $_GET['id'];				
				$user = new User();
				$this->user = $user->field('username,aid')->find($id);
				$this->userAuth = explode(',', $this->user['aid']);
				$auth = new Auth();
				$this->auth = $auth->select();
				$this->render('jurisdiction');				
			}			
			if(IS_POST){
				$id = $_POST['id'];
				$user = new User();
				$user = $user -> findModel($id);
				if(!empty($_POST['aid'])){
					$user -> attributes -> aid = implode($_POST['aid'], ',');
				}				
				$user -> attributes -> ctime = time();
				if($user -> save()){
					echo "<script>alert('权限变动成功');
					window.location.href='index.php?m=admin&c=manager&a=index';</script>";
				}				
			}
			
			
		}		
		
	}
?>